package com.rongshu.view.home.presenter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.view.View;

import com.rongshu.api.RetorfitService;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.home.contract.TodayRecommendView;
import com.rongshu.view.home.entity.ArticleDetails;
import com.rongshu.view.home.entity.ArticleItem;
import com.rongshu.view.home.entity.DaySignInfo;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.widget.toast.ToastUtils;

import java.math.BigDecimal;
import java.util.List;

import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Func1;

public class TodayRecommendPresenter extends BasePresenter<TodayRecommendView> {

    public TodayRecommendPresenter(Context context) {
        super(context);
    }

    /**
     * 获取首页日签
     */
    public void getDaySign(int user_id, String sign) {
        RetorfitService.getDaySign(user_id, sign)
                .compose(getView().<DaySignInfo>bindToLife())
                .subscribe(new Subscriber<DaySignInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(DaySignInfo info) {
                        if (getView() != null) {
                            getView().onDaySign(info);
                        }
                    }
                });
    }

    /**
     * 文章详情
     *
     * @param user_id
     * @param pageIndex
     * @param pageSize
     * @param sign
     */
    public void getArticleDetails(int user_id, int pageIndex, int pageSize, String sign) {
        RetorfitService.getPostDetail(user_id, pageIndex, pageSize, sign)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .map(new Func1<ArticleDetails, List<ArticleItem>>() {
                    @Override
                    public List<ArticleItem> call(ArticleDetails articleDetails) {
                        return articleDetails.getResponse();
                    }
                })
                .map(new Func1<List<ArticleItem>, List<ArticleItem>>() {
                    @Override
                    public List<ArticleItem> call(List<ArticleItem> articleItems) {
                        for (ArticleItem articleItem : articleItems) {
                            if (articleItem != null) {
                                int serialize_id = articleItem.getSerialize_id();
                                if (serialize_id > 0) {
                                    articleItem.setSerial(true);
                                }
                            }
                        }
                        return articleItems;
                    }
                })
                .compose(getView().<List<ArticleItem>>bindToLife())
                .subscribe(new Subscriber<List<ArticleItem>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<ArticleItem> responseBeans) {
                        if (getView() != null) {
                            getView().onArticleDetail(responseBeans);
                        }
                    }
                });
    }

    /**
     * 格式化点赞数
     */
    public String formatNum(int num) {
        String msg = String.valueOf(num);
        BigDecimal b1 = new BigDecimal(String.valueOf(num));
        if (num > 9999) {
            BigDecimal b2 = new BigDecimal("10000");
            double value = b1.divide(b2, 2, BigDecimal.ROUND_DOWN).doubleValue();
            msg = String.valueOf(value);
            return msg + "w";
        } else if (num > 999) {
            BigDecimal b2 = new BigDecimal("1000");
            double value = b1.divide(b2, 2, BigDecimal.ROUND_DOWN).doubleValue();
            msg = String.valueOf(value);
            return msg + "k";
        }
        return msg;
    }

    /**
     * 点赞日签
     */
    public void getDaySignLike(int user_id, int dailyCheckId, int type, String sign) {
        RetorfitService.GetDaySignLike(user_id, dailyCheckId, type, sign)
                .compose(getView().<INetBean>bindToLife())
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean info) {
                        if (getView() != null) {
                            getView().onPraiseDaySignSuccess(info);
                        }
                    }
                });
    }
}
